package com.edie.customview.modular;

import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.edie.customview.R;
import com.edie.customview.custom.PathView;

import java.util.List;

/**
 * - @Description: 区域判断<br/>
 * - @Author: edie<br/>
 * - @Time: 2018/10/8 上午10:39
 */
public class ZoneActivity extends AppCompatActivity {
    private static final String TAG = "ZoneActivity";

    private PathView pathPv;

    public static void start(Context context) {
        context.startActivity(new Intent(context, ZoneActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zone);
        initView();
        pathPv.setOnPathUpdateListener(new PathView.OnPathUpdateListener() {
            @Override
            public void onPathUpdate(List<Point> points) {
                boolean b = PtInPolygon(new Point(500, 500), points);
                Toast.makeText(ZoneActivity.this, "是否包含 " + b, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView() {
        pathPv = (PathView) findViewById(R.id.path_pv);
    }

    /**
     * 功能：判断点是否在多边形内 方法：求解通过该点的水平线与多边形各边的交点 结论：单边交点为奇数，成立!
     *
     * @param point   指定的某个点
     * @param APoints 多边形的各个顶点坐标（首末点可以不一致）
     * @return
     */
    public boolean PtInPolygon(Point point, List<Point> APoints) {
        int nCross = 0;
        for (int i = 0; i < APoints.size(); i++) {
            Point p1 = APoints.get(i);
            Point p2 = APoints.get((i + 1) % APoints.size());
            // 求解 y=p.y 与 p1p2 的交点
            if (p1.y == p2.y)
                // p1p2 与 y=p0.y平行
                continue;
            if (point.y < Math.min(p1.y, p2.y))
                // 交点在p1p2延长线上
                continue;
            if (point.y >= Math.max(p1.y, p2.y))
                // 交点在p1p2延长线上
                continue;
            // 求交点的 X 坐标
            // --------------------------------------------------------------
            double x = (double) (point.y - p1.y) * (double) (p2.x - p1.x) / (double) (p2.y - p1.y) + p1.x;
            if (x > point.x)
                nCross++;
            // 只统计单边交点
        }
        // 单边交点为偶数，点在多边形之外 ---
        return (nCross % 2 == 1);
    }

    /**
     * 判断坐标点是否落在指定的多边形区域内
     *
     * @param point 指定的坐标点
     * @param list  多变形区域的节点集合
     * @return True 落在范围内 False 不在范围内
     */
    public boolean IsWithIn(Point point, List<Point> list) {
        double x = point.x;
        double y = point.y;

        int isum = 0;
        double dLon1, dLon2, dLat1, dLat2, dLon;

        if (list.size() < 3) {
            return false;
        }

        int icount = list.size();

        for (int i = 0; i < icount; i++) {
            if (i == icount - 1) {
                dLon1 = list.get(i).x;
                dLat1 = list.get(i).y;
                dLon2 = list.get(0).x;
                dLat2 = list.get(0).y;
            } else {
                dLon1 = list.get(i).x;
                dLat1 = list.get(i).y;
                dLon2 = list.get(i + 1).x;
                dLat2 = list.get(i + 1).y;
            }

            // 判断指定点的 纬度是否在 相邻两个点(不为同一点)的纬度之间
            if (((y >= dLat1) && (y < dLat2)) || ((y >= dLat2) && (y < dLat1))) {
                if (Math.abs(dLat1 - dLat2) > 0) {
                    dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - y)) / (dLat1 - dLat2);
                    if (dLon < x) {
                        isum++;
                    }
                }
            }
        }
        Log.i(TAG, "IsWithIn: isum= " + isum);
        if ((isum % 2) != 0) {
            return true;
        } else {
            return false;
        }
    }


}
